package com.wanxi.util;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class JDBC {
    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static Connection init(){
        /*String username = "canyin04";
        String pass = "123456";
        String url = "jdbc:mysql://121.199.55.96:3306/canyin04";*/

        String username = "root";
        String pass = "root";
        String url = "jdbc:mysql://127.0.0.1:3306/keji6";

        Connection connection = null;
        try {
            connection = DriverManager.getConnection(url, username, pass);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return connection;
    }

    //查询所有数据
    public ResultSet findAll(String sql, List<Object> data) {
        ResultSet resultSet = null;
        Connection init = init();
        PreparedStatement preparedStatement = null;

        try {
            preparedStatement = init.prepareStatement(sql);
            if(data!=null) {
                for (int i = 1; i <= data.size(); i++) {
                    preparedStatement.setObject(i, data.get(i - 1));
                }
            }
            System.out.println("查询语句"+preparedStatement);
            resultSet = preparedStatement.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return resultSet;

    }

    //跟据用户名与密码查询用户
    public ResultSet findByNameAndPass(String sql,List<Object> data) {
//        String sql = "select * from user where username = ? and password = ?";
        ResultSet resultSet = null;
        try {
            PreparedStatement preparedStatement = init().prepareStatement(sql);
            if(data!=null) {
                for (int i = 0; i < data.size(); i++) {
                    preparedStatement.setObject(i + 1, data.get(i));
                }
            }
            resultSet = preparedStatement.executeQuery();
            return resultSet;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }


    //插入数据
    public int insertUser(String sql,List<Object> data) {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String now = simpleDateFormat.format(new java.util.Date());


        try {
            PreparedStatement preparedStatement = init().prepareStatement(sql);
            if(data!=null) {
                for (int i = 0; i < data.size(); i++) {
                    preparedStatement.setObject(i + 1, data.get(i));
                }
            }
            //增加，删除，修改都可以用该方法，返回值为受影响的条数
            int i = preparedStatement.executeUpdate();
            return i;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }



    //跟据用户id查询数据
    public ResultSet findByID(String sql,List<Object> data){
        ResultSet resultSet = null;

        try {
            PreparedStatement preparedStatement = init().prepareStatement(sql);
            if(data!=null) {
                preparedStatement.setObject(1, data.get(0));
            }
            resultSet = preparedStatement.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return resultSet;
    }

    //更新数据
    public int update(String sql,List<Object> data){

        try {
            PreparedStatement preparedStatement = init().prepareStatement(sql);

            if(data!=null) {
                for (int i = 0; i < data.size(); i++) {
                    preparedStatement.setObject(i + 1, data.get(i));
                }
            }

            System.out.println(preparedStatement.toString());

            int i = preparedStatement.executeUpdate();
            return i;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return 0;
    }

    //删除单条数据
    public  int deleteById(String sql ,List<Object> data){

        try {
            PreparedStatement preparedStatement = init().prepareStatement(sql);
            if(data!=null) {
                preparedStatement.setObject(1, data.get(0));
            }
            int num = preparedStatement.executeUpdate();
            return num;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    //获取记录数
    public int getCount(String sql){
        try {
            PreparedStatement preparedStatement = init().prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            resultSet.next();
            int count = resultSet.getInt("count");
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    //模糊查询
    public ResultSet GlobalSearch(String sql){

        try {
            PreparedStatement preparedStatement = init().prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();

            return resultSet;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    //模糊查询得到的记录条数
    public int getGlobalSearchCount(String sql){

        try {
            PreparedStatement preparedStatement = init().prepareStatement(sql);
            System.out.println("模糊数量"+preparedStatement);
            ResultSet resultSet = preparedStatement.executeQuery();
            resultSet.next();
            int count = resultSet.getInt("count");

            return count;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return 0;
    }

    public static void main(String[] args) {
        List<Object> list = new ArrayList<>();
        list.add("xx or '1'='1'");
        ResultSet all = new JDBC().findAll("select * from news where id = ?", list);

        while (true){
            try {
                if (!all.next()) break;

            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
    }

}
